﻿using Microsoft.Extensions.DependencyInjection;

namespace IOC1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ServiceCollection services = new ServiceCollection();
            services.AddScoped<Ioctest>();
            services.AddScoped<IConfig, ConfigImpl>();
            services.AddScoped<ILog, LogImpl>();
            services.AddScoped<IStorage, StorageImpl>();

            using (var sp = services.BuildServiceProvider())
            {
                Ioctest t = sp.GetRequiredService<Ioctest>();
                t.Test();
            }
        }
    }
}

class Ioctest
{
    private readonly ILog log;
    private readonly IStorage storage;

    public Ioctest(ILog log, IStorage storage)
    {
        this.log = log;
        this.storage = storage;
    }

    public void Test()
    {
        this.log.Logs("开始上传");
        this.storage.Save("你好", "1.txt");
        this.log.Logs("上传完毕");
    }
}

interface ILog
{
    public void Logs(string message);
}

class LogImpl : ILog
{
    public void Logs(string message)
    {
        Console.WriteLine($"日志：{message}");
    }
}

interface IConfig
{
    public string GetValue(string name);
}

class ConfigImpl : IConfig
{
    public string GetValue(string name)
    {
        return $"{name}";
    }
}

interface IStorage
{
    public void Save(string content, string name);
}

class StorageImpl : IStorage
{
    private readonly IConfig config;

    public StorageImpl(IConfig config)
    {
        this.config = config;
    }

    public void Save(string content, string name)
    {
        string c = this.config.GetValue("http://w.com");
        Console.WriteLine($"向服务器{c}上传{name}的{content}");
    }
}